const BLOB_TYPE = {
    doc: "application/msword",
    ppt: "application/vnd.ms-powerpoint",
    pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
    xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    xls: "application/vnd.ms-excel;charset=UTF-8",
    xml: "application/xml",
    txt: "text/plain",
    pdf: "application/pdf",
    svg: "image/svg+xml",
    json: "application/json",
    htm: "text/html;charset=UTF-8",
    html: "text/html;charset=UTF-8",
    ico: "image/vnd.microsoft.icon",
    jpg: "image/jpeg",
    jpeg: "image/jpeg",
    docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=UTF-8",
    png: "image/png",
    config: "application/config",
    zip: "application/zip",
    apk: "application/vnd.android",
    msi: "application/vnd.msign"
}

/**
 * base64转blob
 * @param {String} base64String base64字符串
 * @param {String} type 文件类型
 * @returns {Blob} blob对象
 */
function base64ToBlob(base64String, type) {
    // 移除URL的头部分，即 data:[<mediatype>][;base64],
    const parts = base64String.split(";base64,");
    const base64Data = parts[1] || base64String; // 获取Base64数据
    const contentType = BLOB_TYPE[type] || 'application/octet-stream';
    // 解码Base64字符串为二进制数据
    const byteCharacters = window.atob(base64Data);
    const byteNumbers = new Array(byteCharacters.length);
    for (let i = 0; i < byteCharacters.length; i++) {
        byteNumbers[i] = byteCharacters.charCodeAt(i);
    }

    // 创建Uint8Array
    const byteArray = new Uint8Array(byteNumbers);

    // 创建Blob对象
    return new Blob([byteArray], { type: contentType });
}

module.exports = base64ToBlob